package com.uenpay.im.ui.old.activity.oa;

import com.qiyunxin.android.http.net.HttpInvokeItem;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.HttpInvokeResult;
import com.uenpay.im.entities.RotateImageViewEntity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * 获取广告
 * 
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class GetBlanInvokeItem extends HttpInvokeItem {
	public class GetBlanInvokeItemResult extends HttpInvokeResult {
		public ArrayList<RotateImageViewEntity> arrayList = new ArrayList<RotateImageViewEntity>();
	}

	public GetBlanInvokeItemResult getOutput() {
		return (GetBlanInvokeItemResult) GetResultObject();
	}

	public GetBlanInvokeItem() {
		String url = APIConfiguration.getBaseUrl() + "Cust/Init/adv?"
				+ APIConfiguration.getCustIdAndToken();
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) {
		GetBlanInvokeItemResult result = new GetBlanInvokeItemResult();
		try {
			JSONObject json = new JSONObject(responseBody);
			result.status = json.optInt("status");
			result.msg = json.optString("msg");

			JSONArray array = json.optJSONArray("data");
			if (array != null && array.length() > 0) {
				for (int i = 0, size = array.length(); i < size; i++) {
					result.arrayList.add(desRotateImageViewEntity(array
							.optJSONObject(i)));
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return result;
		}

		return result;
	}

	private RotateImageViewEntity desRotateImageViewEntity(JSONObject json) {
		RotateImageViewEntity mRotateImageViewEntity = new RotateImageViewEntity();
		mRotateImageViewEntity.pic_url = json.optString("img");
		mRotateImageViewEntity.web_url = json.optString("url");
		return mRotateImageViewEntity;
	}

}
